﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using EduFormManager.Models;

namespace EduFormManager.Utils
{
    public static class GuiHelper
    {
        public struct FormStatusCount
        {
            public int FormsOkCount;
            public int FormsWithErrorsCount;
            public int FormsWithExpiredDateCount;
            public int FormsWithBothCount;
        }
        
        
        public static SynchronizationContext GuiThreadContext 
        { 
            get; 
            set; 
        }

        public static Dictionary<string, TileContainer> CreateTileContainers(Credentials user, WindowsUIView view)
        {
            Dictionary<string, TileContainer> dict = new Dictionary<string, TileContainer>();
            TileContainer formContainer;

            if (user.IsAdmin || user.IsMinistry)
            {
                formContainer = LevelAdminHelper.CreateEduFormsTileContainer(view, AllformType.Type.Types.Federal, false);
                dict.Add(TagHelper.FEDERAL_EDU_FORMS_TAG, formContainer);

                formContainer = LevelAdminHelper.CreateEduFormsTileContainer(view, AllformType.Type.Types.Federal, true);
                dict.Add(TagHelper.ARCHIVE_FEDERAL_EDU_FORMS_TAG, formContainer);

                formContainer = LevelAdminHelper.CreateEduFormsTileContainer(view, AllformType.Type.Types.Other, false);
                dict.Add(TagHelper.ADDON_EDU_FORMS_TAG, formContainer);

                formContainer = LevelAdminHelper.CreateEduFormsTileContainer(view, AllformType.Type.Types.Other, true);
                dict.Add(TagHelper.ARCHIVE_ADDON_EDU_FORMS_TAG, formContainer);

                formContainer = LevelAdminHelper.CreateMunicipalityFormsTileContainer(view, false);
                dict.Add(TagHelper.FEDERAL_MUNIT_FORMS_TAG, formContainer);

                formContainer = LevelAdminHelper.CreateMunicipalityFormsTileContainer(view, true);
                dict.Add(TagHelper.ARCHIVE_FEDERAL_MUNIT_FORMS_TAG, formContainer);
            }
            else if (user.IsMunicipality)
            {
                Municipality munit = user.Municipality;

                formContainer = LevelMunitHelper.CreateEduFormsTileContainer(munit, view, AllformType.Type.Types.Federal, false);
                dict.Add(TagHelper.FEDERAL_EDU_FORMS_TAG, formContainer);

                formContainer = LevelMunitHelper.CreateEduFormsTileContainer(munit, view, AllformType.Type.Types.Federal, true);
                dict.Add(TagHelper.ARCHIVE_FEDERAL_EDU_FORMS_TAG, formContainer);

                formContainer = LevelMunitHelper.CreateEduFormsTileContainer(munit, view, AllformType.Type.Types.Other, false);
                dict.Add(TagHelper.ADDON_EDU_FORMS_TAG, formContainer);

                formContainer = LevelMunitHelper.CreateEduFormsTileContainer(munit, view, AllformType.Type.Types.Other, true);
                dict.Add(TagHelper.ARCHIVE_ADDON_EDU_FORMS_TAG, formContainer);

                formContainer = LevelMunitHelper.CreateMunicipalityFormsTileContainer(munit, view, false);
                dict.Add(TagHelper.FEDERAL_MUNIT_FORMS_TAG, formContainer);

                formContainer = LevelMunitHelper.CreateMunicipalityFormsTileContainer(munit, view, true);
                dict.Add(TagHelper.ARCHIVE_FEDERAL_MUNIT_FORMS_TAG, formContainer);
            }
            else if (user.IsEdu)
            {
                EduOrg edu = user.EduOrg;

                formContainer = LevelEduHelper.CreateEduFormsTileContainer(edu, view, AllformType.Type.Types.Federal, false);
                dict.Add(TagHelper.FEDERAL_EDU_FORMS_TAG, formContainer);

                formContainer = LevelEduHelper.CreateEduFormsTileContainer(edu, view, AllformType.Type.Types.Federal, true);
                dict.Add(TagHelper.ARCHIVE_FEDERAL_EDU_FORMS_TAG, formContainer);

                formContainer = LevelEduHelper.CreateEduFormsTileContainer(edu, view, AllformType.Type.Types.Other, false);
                dict.Add(TagHelper.ADDON_EDU_FORMS_TAG, formContainer);

                formContainer = LevelEduHelper.CreateEduFormsTileContainer(edu, view, AllformType.Type.Types.Other, true);
                dict.Add(TagHelper.ARCHIVE_ADDON_EDU_FORMS_TAG, formContainer);
            }
            else
            {
                throw new NotSupportedException("Unavailable user type was passed into.");
            }
            if (user.IsAdmin || user.IsMinistry || user.IsMunicipality)
            {
                TileContainer questionaryContainer = LevelAdminHelper.CreateQuestionaryTileContainer(view);
                dict.Add(TagHelper.QUESTIONARY_TAG, questionaryContainer);
            }
            return dict;
        }
        public static Task<Dictionary<string, TileContainer>> CreateTileContainersAsync(Credentials user, WindowsUIView view)
        {
            return Task.Run(() => CreateTileContainers(user, view));
        }
    }
}
